package com.aptech.oas.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.filters.AddDefaultCharsetFilter;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.aptech.oas.model.User;
import com.aptech.oas.model.Vacation;
import com.aptech.oas.util.SessionFactoryUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class VacationAction extends ActionSupport {

	//申请休假，提交后，submit.action存放表单提交的用户名
	private String username;
	//经理审核时，申请人工号提交到audit.action
	private long vid;
	//经理审核 休假申请单，给出的审核意见，1代表通过，2代表拒绝
	private int answer;
	//申请休假，提交后，submit.action存放表单提交的申请休假单的信息。
	private Vacation vacation = new Vacation();

	public String vacation() {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> s = context.getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		SessionFactory sf = SessionFactoryUtil.get();
		Session session = sf.openSession();
		if ((boolean) s.get("userAuthority")) {
			Query q = session.createQuery("from Vacation v where v.applicant= ? or v.approver= ?");
			q.setString(0, (String) s.get("loginUser"));
			q.setString(1, (String) s.get("loginUser"));
			List<Vacation> vas = (List<Vacation>) q.list();
			request.setAttribute("vas", vas);
			session.close();
			return "manager";
		}
		Query q = session.createQuery("from Vacation v where v.applicant= ?");
		q.setString(0, (String) s.get("loginUser"));
		List<Vacation> vas = (List<Vacation>) q.list();
		request.setAttribute("vas", vas);
		session.close();
		return "employee";
	}

	public String apply() {
		SessionFactory sf = SessionFactoryUtil.get();
		Session session = sf.openSession();
		Query q = session.createQuery("from User u where u.authority=true");
		List<User> managers = (List<User>) q.list();
		session.close();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("managers", managers);
		return SUCCESS;
	}

	public String submit() {
		if (vacation.getApprover() != null && vacation.getStartTime() != null && vacation.getEndTime() != null
				&& vacation.getDays() != null && vacation.getEndTime().getTime() >= vacation.getStartTime().getTime()) {
			ActionContext context = ActionContext.getContext();
			String workNo = (String) context.getSession().get("loginUser");
			SessionFactory sf = SessionFactoryUtil.get();
			Session session = sf.openSession();
			User applicant = session.get(User.class, workNo);
			if (!applicant.getUsername().equalsIgnoreCase(username)) {
				this.addFieldError("username error", "用户名错误");
				return "paramerror";
			}
			Date now = new Date();
			if (vacation.getStartTime().getTime() < now.getTime() + 1) {
				this.addFieldError("startTime error", "开始时间必须大于今天");
				return "paramerror";
			}
			now.setTime(vacation.getEndTime().getTime());
			long endTimeMS = now.getTime() + 24 * 60 * 60 * 1000;
			now.setTime(endTimeMS);
			long d = now.getTime() - vacation.getStartTime().getTime();
			long days = vacation.getDays() * 24 * 60 * 60 * 1000L;
			if (d != days) {
				addFieldError("days error", "申请的天数与日期不符");
				return "paramerror";
			}
			vacation.setApplicant(applicant);

			session.beginTransaction();
			session.save(vacation);
			session.getTransaction().commit();
			session.close();
			return SUCCESS;
		}
		this.addFieldError("null parameter", "输入值不能为空");
		return "paramerror";
	}

	public String audit() {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> s = context.getSession();
		if(vid==0){
			addFieldError("applicant error", "申请人信息出错");
			return INPUT;
		}
		if ((boolean) s.get("userAuthority")) {
			String workNo = (String) s.get("loginUser");
			SessionFactory sf = SessionFactoryUtil.get();
			Session session=sf.openSession();
			Vacation v=session.get(Vacation.class, vid);
			session.close();
			HttpServletRequest request=ServletActionContext.getRequest();
			request.setAttribute("v", v);
			request.setAttribute("vid", vid);
			return SUCCESS;
		}
		addFieldError("authority error", "您没有权限");
		return "input";
	}
	
	public String auditResult(){
		if(answer==0){
			addFieldError("audit error", "审核出错");
			return INPUT;
		}
		if(vid==0){
			addFieldError("audit error", "信息出错");
			return INPUT;
		}
		SessionFactory sf=SessionFactoryUtil.get();
		Session session=sf.openSession();
		session.beginTransaction();
		Vacation v=session.get(Vacation.class, vid);
		v.setStatus((byte) answer);
		session.save(v);
		session.getTransaction().commit();
		session.close();
		return SUCCESS;
	}

	public String test() {
		return SUCCESS;
	}

	public Vacation getVacation() {
		return vacation;
	}

	public void setVacation(Vacation vacation) {
		this.vacation = vacation;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}



	public long getVid() {
		return vid;
	}

	public void setVid(long vid) {
		this.vid = vid;
	}

	public int getAnswer() {
		return answer;
	}

	public void setAnswer(int answer) {
		this.answer = answer;
	}
}
